package com.sansan.qiangji.utils;

public class DataUtil {
    public static byte[] int2byte(int i) {
        byte[] targets = new byte[2];
        for (int j = 0; j < 2; j++) {
            int offset = (targets.length - 1 - j) * 8;
            targets[j] = (byte) ((i >>> offset) & 0xff);
        }
        return targets;
    }

    public static byte bit2byte(String bString){
        byte result=0;
        for(int i=bString.length()-1,j=0;i>=0;i--,j++){
            result+=(Byte.parseByte(bString.charAt(i)+"")*Math.pow(2, j));
        }
        return result;
    }

    public static byte[] short2Byte(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();
            temp = temp >> 8;
        }
        return b;
    }
}
